#!/usr/bin/make -f

include Makefile

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  NUMJOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  MAKEFLAGS += -j$(NUMJOBS)
endif

xkbdir := /usr/share/X11/xkb

pre := debian/preprocessor
manprocessor := sed \
	-e 's|^\([.a-zA-Z][a-zA-Z]*\) /usr/local/etc|\1 /etc|' \
	-e 's|^\([.a-zA-Z][a-zA-Z]*\) /usr/local|\1 /usr|'

# One font per fontset supported by d-i in text console
udeb-fonts := CyrAsia-Fixed16 CyrSlav-TerminusBold16 Greek-VGA16 \
Lat15-TerminusBold16 Lat2-TerminusBold16 Lat38-VGA16 Lat7-TerminusBold16
# These are Linux-only
udeb-fonts-big := 

# These are not properly supported by d-i in text console, but
# included for completeness for speakup & brltty screen reading
udeb-fonts += Armenian-Fixed16 Georgian-Fixed16 Hebrew-VGA16 Lao-Fixed16 Thai-Fixed16
udeb-fonts-big += Arabic-VGA16 Ethiopian-Goha16 Vietnamese-Fixed16

# These are needed for proper text screen reading by speakup and brltty
udeb-fonts-big += FullCyrAsia-Fixed16 FullCyrSlav-TerminusBold16 FullGreek-VGA16

# This is not useful (KOI8-R encoding)
# CyrKoi-TerminusBold16

# These are not useful (convered by fonts above)
# Uni1-VGA16 Uni2-VGA16 Uni3-Fixed16

# One fontset per charmap
udeb-scm := $(addprefix Fonts/, ARMSCII-8_Armenian.scm			\
CP1251_CyrSlav.scm CP1255_Hebrew.scm GEORGIAN-ACADEMY_Georgian.scm	\
GEORGIAN-PS_Georgian.scm IBM1133_Lao.scm ISO-8859-1_Lat15.scm		\
ISO-8859-2_Lat2.scm ISO-8859-3_Lat38.scm ISO-8859-4_Lat7.scm		\
ISO-8859-4_Lat15.scm ISO-8859-5_CyrSlav.scm ISO-8859-7_Greek.scm	\
ISO-8859-8_Hebrew.scm ISO-8859-9_Lat15.scm ISO-8859-10_Lat15.scm	\
ISO-8859-11_Thai.scm ISO-8859-13_Lat7.scm ISO-8859-14_Lat38.scm		\
ISO-8859-15_Lat15.scm ISO-8859-16_Lat2.scm KOI8-R_CyrKoi.scm		\
KOI8-U_CyrKoi.scm TIS-620_Thai.scm)

udeb-fonts-linux = $(addprefix Fonts/, $(addsuffix .psf.gz, \
			$(udeb-fonts) $(udeb-fonts-big)))
udeb-fonts-freebsd = $(addprefix Fonts/, $(addsuffix .fnt, $(udeb-fonts)))

debian/kbdnames.gz: $./Keyboard/KeyboardNames.pl
	( cd Keyboard \
	&& ./kbdnames-maker ./KeyboardNames.pl \
		| grep -v '^C[*]' | grep -v '[*]model[*]' | sort | gzip -9n ) >$@

models := amiga ataritt macintosh_old pc105 sun4 sun5

di-gzipped-ekmaps := $(addprefix $./, $(addsuffix .ekmap.gz, $(models)))

di-gzipped-ekbds := $./pc105.ekbd.gz

# di-kbdnames = us al ara es(ast) bd by in(ben) be ba br gb bg bg(phonetic)...
di-kbdnames := $(shell grep -A 2 keyboard-configuration/xkb-keymap debian/console-setup-udeb.templates | tail -n 1 | cut -d ' ' -f 2- | tr -d , | tr ' ' '\n' | grep -v skip-config)

$./%.ekmap : model = $(notdir $(@:%.ekmap=%))
$./%.ekmap :
	(cd Keyboard \
	&& ./kbdcompiler $(model) ./KeyboardNames.pl $(xkbdir) \
		"$(di-kbdnames)") >$@

$./%.ekbd : model = $(notdir $(@:%.ekbd=%))
$./%.ekbd :
	(cd Keyboard \
	&& ./kbdcompiler freebsd-$(model) ./KeyboardNames.pl $(xkbdir) \
		"$(di-kbdnames)") >$@

# The following rules ensure that kbdcompiler is not run
# simultaneously for normal and udeb kmaps
$(foreach i,$(gzipped-ekmaps:%.gz=%) $(gzipped-ekbds:%.gz=%),\
	$(eval $i: $(maindir)/$(notdir $i)))

# A hack so that KeyboardNames.pl is recomputed using the files in
# /usr/share/X11/xkb instead of the files shipped with console-setup
$(gzipped-ekmaps:%.gz=%) $(gzipped-ekbds:%.gz=%) $(di-gzipped-ekmaps:%.gz=%) $(di-gzipped-ekbds:%.gz=%) : $./Keyboard/KeyboardNames.pl
$./Keyboard/KeyboardNames.pl : building_debian
building_debian :
	touch building_debian

build: debian/kbdnames.gz $(di-gzipped-ekmaps) $(di-gzipped-ekbds)

clean : maintainer-clean
	dh_testdir
	-rm -f *.ekmap *.ekbd *.ekmap.gz *.ekbd.gz
	debconf-updatepo
	-rm -f build main_build building_debian
	-rm -f debian/kbdnames.gz
	-rm -f debian/*~
	dh_clean

.PHONY : deb-udeb-install
deb-udeb-install: deb-install-linux deb-install-freebsd deb-install-main deb-install-bdf2psf udeb-install-linux udeb-install-freebsd udeb-install

.PHONY : deb-install-linux
deb-install-linux: build
	dh_testdir
	dh_testroot
	dh_prep -p console-setup-linux
	$(MAKE) etcdir=debian/console-setup-linux/etc \
		prefix=debian/console-setup-linux/usr install-common-linux
	dh_install -p console-setup-linux \
		init/90-console-setup.rules lib/udev/rules.d/
	dh_install -p console-setup-linux \
		init/keyboard-setup.sh lib/console-setup/
	dh_install -p console-setup-linux \
		init/console-setup.sh lib/console-setup/
	dh_systemd_enable -p console-setup-linux \
		--name keyboard-setup
	dh_systemd_enable -p console-setup-linux \
		--name console-setup
	dh_installinit -p console-setup-linux \
		--no-start --name keyboard-setup.sh
	dh_installinit -p console-setup-linux \
		--no-start --name console-setup.sh
	dh_systemd_start -p console-setup-linux \
		--no-start --name keyboard-setup
	dh_systemd_start -p console-setup-linux \
		--no-start --name console-setup
	sed -i 's/RUNLEVEL/2 3 4 5/' \
		debian/console-setup-linux/etc/init.d/console-setup.sh

.PHONY : deb-install-freebsd
deb-install-freebsd: build
	dh_testdir
	dh_testroot
	dh_prep -p console-setup-freebsd
	$(MAKE) etcdir=debian/console-setup-freebsd/etc \
		prefix=debian/console-setup-freebsd/usr install-common-freebsd
	dh_install -p console-setup-freebsd \
		init/keyboard-setup.sh lib/console-setup/
	dh_install -p console-setup-freebsd \
		init/console-setup.sh lib/console-setup/
	dh_systemd_enable -p console-setup-freebsd \
		--name keyboard-setup
	dh_systemd_enable -p console-setup-freebsd \
		--name console-setup
	dh_installinit -p console-setup-freebsd \
		--no-start --name keyboard-setup.sh
	dh_installinit -p console-setup-freebsd \
		--no-start --name console-setup.sh
	dh_systemd_start -p console-setup-freebsd \
		--no-start --name keyboard-setup
	dh_systemd_start -p console-setup-freebsd \
		--no-start --name console-setup
	sed -i 's/RUNLEVEL/S/' \
		debian/console-setup-freebsd/etc/init.d/console-setup.sh

.PHONY : deb-install-main
deb-install-main: build
	dh_testdir
	dh_testroot
	dh_prep -pkeyboard-configuration
	dh_prep -p console-setup
	dh_prep -p console-setup-mini
	$(MAKE) etcdir=debian/console-setup/etc \
		prefix=debian/console-setup/usr install-common
	$(MAKE) etcdir=debian/console-setup/etc \
		prefix=debian/console-setup/usr install-ckbcomp
	$(MAKE) etcdir=debian/console-setup-mini/etc \
		prefix=debian/console-setup-mini/usr install-common
	$(MAKE) etcdir=debian/console-setup-mini/etc \
		prefix=debian/console-setup-mini/usr install-ckbcomp-mini
	$(pre) --mini debian/console-setup-mini/bin/setupcon
	$(pre) --mini debian/console-setup-mini/usr/bin/ckbcomp-mini
	ls debian/console-setup/usr/share/man/*/* \
	   debian/console-setup-mini/usr/share/man/*/* \
		| xargs -n 1 $(manprocessor) -i
	install -d debian/keyboard-configuration/usr/share/console-setup/
	mv debian/console-setup/etc/default/keyboard \
		debian/keyboard-configuration/usr/share/console-setup/
	install -d debian/console-setup/usr/share/console-setup/
	mv debian/console-setup/etc/default/console-setup \
		debian/console-setup/usr/share/console-setup/
	rm debian/console-setup-mini/etc/default/keyboard
	rm debian/console-setup-mini/etc/default/console-setup
	dh_movefiles -pkeyboard-configuration --sourcedir=debian/console-setup \
		usr/share/man/man5/keyboard.5
	rm debian/console-setup-mini/usr/share/man/man5/keyboard.5
# For KeyboardNames.pl and kbdnames-maker see #420914
	dh_install -pkeyboard-configuration Keyboard/KeyboardNames.pl \
		usr/share/console-setup/
	dh_install -pkeyboard-configuration Keyboard/kbdnames-maker \
		usr/share/console-setup/
	dh_link -pkeyboard-configuration usr/share/X11/xkb/rules/xorg.lst \
		usr/share/doc/keyboard-configuration/xorg.lst

.PHONY : deb-install-bdf2psf
deb-install-bdf2psf: build
	dh_testdir
	dh_testroot
	dh_prep -pbdf2psf
	dh_install -pbdf2psf Fonts/bdf2psf usr/bin/
	dh_install -pbdf2psf Fonts/*.equivalents Fonts/*.set \
		Fonts/fontsets usr/share/bdf2psf
	dh_installman -pbdf2psf man/bdf2psf.1
	dh_installdocs -pbdf2psf debian/README.fontsets

.PHONY : udeb-install-linux
udeb-install-linux: build
	dh_testdir
	dh_testroot
# console-setup-linux-charmaps-udeb
	dh_prep -p console-setup-linux-charmaps-udeb
	$(MAKE) etcdir=debian/console-setup-linux-charmaps-udeb/etc \
		prefix=debian/console-setup-linux-charmaps-udeb/usr \
			install-common-linux
	dh_install -p console-setup-linux-charmaps-udeb \
		Keyboard/charmap_functions.sh usr/share/console-setup/
	rm -r debian/console-setup-linux-charmaps-udeb/usr/share/consolefonts/
# console-setup-linux-fonts-udeb
	dh_prep -p console-setup-linux-fonts-udeb
	dh_install -p console-setup-linux-fonts-udeb $(udeb-fonts-linux) \
		usr/share/consolefonts
	dh_install -p console-setup-linux-fonts-udeb debian/font-switch \
		/usr/share/console-setup

.PHONY : udeb-install-freebsd
udeb-install-freebsd: build
	dh_testdir
	dh_testroot
# console-setup-freebsd-charmaps-udeb
	dh_prep -p console-setup-freebsd-charmaps-udeb
	$(MAKE) etcdir=debian/console-setup-freebsd-charmaps-udeb/etc \
		prefix=debian/console-setup-freebsd-charmaps-udeb/usr \
			install-common-freebsd
	dh_install -p console-setup-freebsd-charmaps-udeb \
		Keyboard/charmap_functions.sh usr/share/console-setup/
	rm -r debian/console-setup-freebsd-charmaps-udeb/usr/share/consoletrans/
	rm -r debian/console-setup-freebsd-charmaps-udeb/usr/share/syscons/
# console-setup-freebsd-fonts-udeb
	dh_prep -p console-setup-freebsd-fonts-udeb
	dh_install -p console-setup-freebsd-fonts-udeb $(udeb-fonts-freebsd) \
		usr/share/syscons/fonts/
	dh_install -p console-setup-freebsd-fonts-udeb $(udeb-scm) \
		usr/share/syscons/scrnmaps/

.PHONY : udeb-install
udeb-install: build
	dh_testdir
	dh_testroot
	dh_prep -p console-setup-amiga-ekmap
	dh_prep -p console-setup-ataritt-ekmap
	dh_prep -p console-setup-macintoshold-ekmap
	dh_prep -p console-setup-pc-ekmap
	dh_prep -p console-setup-sun4-ekmap
	dh_prep -p console-setup-sun5-ekmap
	dh_prep -p console-setup-udeb
	dh_prep -p console-setup-pc-ekbd
	$(MAKE) etcdir=debian/console-setup-udeb/etc \
		prefix=debian/console-setup-udeb/usr install-common
	$(MAKE) etcdir=debian/console-setup-udeb/etc \
		prefix=debian/console-setup-udeb/usr install-ckbcomp-mini
	$(pre) --udeb debian/console-setup-udeb/bin/setupcon
	$(pre) --udeb debian/console-setup-udeb/usr/bin/ckbcomp-mini
	rm -r debian/console-setup-udeb/usr/share/man/
	dh_installdirs -p console-setup-udeb usr/share/console-setup
	mv debian/console-setup-udeb/etc/default/keyboard \
		debian/console-setup-udeb/usr/share/console-setup/
	rm debian/console-setup-udeb/etc/default/console-setup
	rm debian/console-setup-udeb/usr/share/console-setup/charmap_functions.sh
	install -m0755 debian/keyboard-configuration.config \
		debian/console-setup-udeb/usr/share/console-setup/
	$(pre) --udeb debian/console-setup-udeb/usr/share/console-setup/keyboard-configuration.config
	dh_installdirs -p console-setup-udeb lib/debian-installer.d/
	install -m0644 debian/console-setup-udeb.startup \
		debian/console-setup-udeb/lib/debian-installer.d/S55console-setup
	dh_installdirs -p console-setup-udeb usr/lib/base-installer.d
	install -m0755 debian/console-setup-udeb.base-installer \
		debian/console-setup-udeb/usr/lib/base-installer.d/20console-setup
	$(pre) --udeb debian/console-setup-udeb/usr/lib/base-installer.d/20console-setup
# console-setup-*-ekmap
	dh_movefiles -p console-setup-amiga-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/amiga.ekmap.gz
	cat amiga.ekmap.gz >debian/console-setup-amiga-ekmap/usr/share/console-setup/amiga.ekmap.gz
	dh_movefiles -p console-setup-ataritt-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/ataritt.ekmap.gz
	cat ataritt.ekmap.gz >debian/console-setup-ataritt-ekmap/usr/share/console-setup/ataritt.ekmap.gz
	dh_movefiles -p console-setup-macintoshold-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/macintosh_old.ekmap.gz
	cat macintosh_old.ekmap.gz >debian/console-setup-macintoshold-ekmap/usr/share/console-setup/macintosh_old.ekmap.gz
	dh_movefiles -p console-setup-pc-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/pc105.ekmap.gz
	cat pc105.ekmap.gz >debian/console-setup-pc-ekmap/usr/share/console-setup/pc105.ekmap.gz
	dh_movefiles -p console-setup-sun4-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/sun4.ekmap.gz
	cat sun4.ekmap.gz >debian/console-setup-sun4-ekmap/usr/share/console-setup/sun4.ekmap.gz
	dh_movefiles -p console-setup-sun5-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/sun5.ekmap.gz
	cat sun5.ekmap.gz >debian/console-setup-sun5-ekmap/usr/share/console-setup/sun5.ekmap.gz
# console-setup-pc-ekbd
	dh_movefiles -p console-setup-pc-ekbd --sourcedir=debian/console-setup-udeb usr/share/console-setup/pc105.ekbd.gz
	cat pc105.ekbd.gz >debian/console-setup-pc-ekbd/usr/share/console-setup/pc105.ekbd.gz

# Build architecture-independent files here.
.PHONY : binary-indep
binary-indep: build deb-udeb-install
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_installdocs -pkeyboard-configuration \
		FAQ copyright.fonts copyright.xkb
	dh_installdocs -p console-setup-linux --link-doc=keyboard-configuration
	dh_installdocs -pconsole-setup-freebsd --link-doc=keyboard-configuration
	dh_installdocs -p console-setup-mini --link-doc=keyboard-configuration
	dh_installdocs -p console-setup --link-doc=keyboard-configuration
	dh_lintian
	dh_bugfiles
	dh_installman
	dh_installchangelogs
	dh_compress
	dh_fixperms
	dh_installdeb
# To deal somehow with bug #657904.  See also #255819 and #17624.
#	rm debian/console-setup/usr/share/doc/console-setup
	$(pre) debian/keyboard-configuration/DEBIAN/config
	$(pre) debian/keyboard-configuration/DEBIAN/postinst
	$(pre) debian/console-setup/DEBIAN/config
	$(pre) debian/console-setup/DEBIAN/postinst
	$(pre) --udeb debian/console-setup-udeb/DEBIAN/postinst
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
.PHONY : binary-arch
binary-arch : build deb-udeb-install
# We have nothing to do by default.

.PHONY : binary
binary : binary-indep binary-arch

.PHONY : build-arch build-indep build-stamp
build-arch build-indep build-stamp : build
